//
//  YJRegisterViewController.m
//  Sports-Go
//
//  Created by rimi on 16/9/23.
//  Copyright © 2016年 YRL. All rights reserved.
//

#import "YJRegisterViewController.h"
#import "YJCateAnimation.h"
@interface YJRegisterViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UIButton *registerButton;
@property (nonatomic,strong) UITextField *userTextField;
@property (nonatomic,strong) UITextField *passwordField;
@property (nonatomic,strong) UITextField *mailBoxField;
@property (nonatomic,strong) YJCateAnimation *animals;
@property (assign,nonatomic) ClickType1 clicktype;
@end
@implementation YJRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _animals = [[YJCateAnimation alloc] init];
    _animals.frame = RzCGRectMake(0, 50, 375, 400);

    UIImageView *backImage = [[UIImageView alloc] initWithFrame:RzCGRectMake(0, 0, 375, 667)];
    
    backImage.image = [UIImage imageNamed:@"loginback"];
    [self.view addSubview:backImage];

    UIButton *backButton = [[UIButton alloc] initWithFrame:RzCGRectMake(30, 50, 50, 30)];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backtheLastPage) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIView *maskView = [[UIView alloc] initWithFrame:RzCGRectMake(30, 250, 315, 150)];
    maskView.backgroundColor = getColor(58, 206, 248, 1);
    //用户名label
   UILabel *userLabel = [[UILabel alloc] initWithFrame:RzCGRectMake(5, 10, 70, 40)];
   userLabel .text = @"用户名:";
    userLabel.textColor = [UIColor whiteColor];
    //密码label
   UILabel *passLabel = [[UILabel alloc] initWithFrame:RzCGRectMake(5, 55, 70, 40)];
    passLabel.text = @"密   码:";
    passLabel.textColor = [UIColor whiteColor];
    //邮箱label
    UILabel *mailboxLabel = [[UILabel alloc] initWithFrame:RzCGRectMake(5, 100, 70, 40)];
    mailboxLabel.text = @"邮   箱:";
    mailboxLabel.textColor = [UIColor whiteColor];
    
    [self.view addSubview:_animals];
    [self.view addSubview:backButton];
    [maskView addSubview:userLabel];
    [maskView addSubview:passLabel];
    [maskView addSubview:mailboxLabel];
    [maskView addSubview:self.userTextField];
    [maskView addSubview:self.passwordField];
    [maskView addSubview:self.mailBoxField];
    [self.view addSubview:maskView];
    [self.view addSubview:self.registerButton];

}
#pragma mark -- button click
- (void)backtheLastPage{
    
    [self.navigationController popViewControllerAnimated:YES];
}
//注册
- (void)sureRegister:(UIButton *)sender{
    
    if ([self.userTextField.text isEqualToString:@""] || [self.passwordField.text isEqualToString:@""] ||[self.mailBoxField.text isEqualToString:@""]) {
        //为空
        if ([self.userTextField.text isEqualToString:@""]) {
            [self warnWitherrorInfo:@"用户名不可为空"];
            [self.userTextField becomeFirstResponder];
        }
        else if ([self.passwordField.text isEqualToString:@""]) {
            [self warnWitherrorInfo:@"密码不可为空"];
            [self.passwordField becomeFirstResponder];
        }
        else if ([self.mailBoxField.text isEqualToString:@""]) {
            [self warnWitherrorInfo:@"邮箱不可为空"];
            [self.mailBoxField becomeFirstResponder];
        }
        
    }else{
       
        BOOL ifMailBox =  [self validateEmail:self.mailBoxField.text];
        if (ifMailBox) {
            //是邮箱
          
            AVUser *user = [AVUser user];
            user.username = self.userTextField.text;
            user.password = self.passwordField.text;
            user.email = self.mailBoxField.text;
            
            [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (error) {
                    //网络连接失败
                    [self warnWitherrorInfo:@"网络连接失败"];
                }else{
                  
                    if (succeeded) {
                        //注册成功
                        
                        [self warnWitherrorInfo:@"注册成功,请先去邮箱激活"];
                        [self performSelector:@selector(backtheLastPage) withObject:self afterDelay:3];
                        
                    }else{
        
                        [self warnWitherrorInfo:@"注册失败"];
                        //注册失败
                    }
                }
                
            }];
        }else{
            //不是邮箱
            [self warnWitherrorInfo:@"请输入正确的邮箱"];
            _mailBoxField.text = @"";
            [_mailBoxField becomeFirstResponder];
            
        }

    }
}

#pragma mark -- uitextfield delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{

    if (textField.tag == 503) {
        if (_clicktype == clicktypePass1) {
            _clicktype = clicktypePass1;
            return;
        }
        _clicktype = clicktypePass1;
        [_animals closeTheEyes];
    }else if(textField.tag == 502) {
        if (_clicktype != clicktypePass1) {
            _clicktype = clicktypeUser1;
            return;
        }
        _clicktype = clicktypeUser1;
        [_animals openTheEyes];
    }else{
        if (_clicktype != clicktypePass1) {
            _clicktype = clicktypeUser1;
            return;
        }
        _clicktype = clicktypeUser1;
        [_animals openTheEyes];
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField{

    if (textField.tag == 504) {
        if (textField.text.length > 0) {
         
        }
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField.tag == 503) {
    
    //敲删除键
    if ([string length] == 0) {
        return YES;
    }
    if (textField.text.length> 11) {
        [self warnWitherrorInfo:@"密码设置不可太长"];
        return NO;
    }
    NSMutableString *fieldText = [NSMutableString stringWithFormat:@"%@", textField.text];
     [fieldText replaceCharactersInRange:range withString:string];
     NSString *finalText=[fieldText copy];
    //如果字符个数大于18就要进行截取前边18个字符
    if ([finalText length]>11) {
        textField.text=[finalText substringToIndex:11];
        return NO;
    }

    }
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if ([textField isEqual:_userTextField]) {
        [_passwordField becomeFirstResponder];
    }else if ([textField isEqual:_passwordField]){
    
        [_mailBoxField becomeFirstResponder];
    }else{
       [textField resignFirstResponder];
    }
    
    return YES;
}

#pragma mark -- alter
- (void)warnWitherrorInfo:(NSString *)error{
    //警示框
    UILabel *label = [[UILabel alloc]initWithFrame:RzCGRectMake(100, 270, 185, 50)];
    label.backgroundColor = [UIColor lightGrayColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:16];
    label.numberOfLines = 0;
    label.text = error;
    label.layer.cornerRadius = 5;
    label.layer.masksToBounds = YES;
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    [UIView animateWithDuration:2.0 animations:^{
        label.alpha = 0.0;
    } completion:^(BOOL finished) {
        
        [label removeFromSuperview];
    }];
    
    
}
//邮箱
- (BOOL) validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

#pragma mark - lazy
- (UIButton *)registerButton{
    if (!_registerButton) {
        _registerButton = [[UIButton alloc] initWithFrame:RzCGRectMake(375/3.0, 425, 375/3.0, 40)];
        [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
        
        [_registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _registerButton.backgroundColor = getColor(193, 162, 151, 1);
        _registerButton.layer.cornerRadius = 5;
        _registerButton.layer.masksToBounds = YES;
        [_registerButton addTarget:self action:@selector(sureRegister:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerButton;
}
- (UITextField *)userTextField{
    
    if (!_userTextField) {
        _userTextField = [[UITextField alloc] initWithFrame:RzCGRectMake(75, 10, 230, 40)];
        _userTextField.backgroundColor = [UIColor whiteColor];
        _userTextField.layer.cornerRadius = 5;
        _userTextField.layer.masksToBounds = YES;
        _userTextField.delegate = self;
        _userTextField.tag = 502;
        _userTextField.placeholder = @"请输入用户名";
        _userTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        
    }
    return _userTextField;
}

- (UITextField *)passwordField{
    
    if (!_passwordField) {
        _passwordField = [[UITextField alloc] initWithFrame:RzCGRectMake(75, 55, 230, 40)];
        _passwordField.backgroundColor = [UIColor whiteColor];
        _passwordField.layer.cornerRadius = 5;
        _passwordField.layer.masksToBounds = YES;
        _passwordField.delegate = self;
        _passwordField.tag = 503;
        _passwordField.placeholder = @"请输入密码";
        _passwordField.secureTextEntry = YES;
        _passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _passwordField;
}
- (UITextField *)mailBoxField{
    if (!_mailBoxField) {
        _mailBoxField = [[UITextField alloc] initWithFrame:RzCGRectMake(75, 100, 230, 40)];
        _mailBoxField.backgroundColor = [UIColor whiteColor];
        _mailBoxField.layer.cornerRadius = 5;
        _mailBoxField.layer.masksToBounds = YES;
        _mailBoxField.delegate = self;
        _mailBoxField.tag = 504;
        _mailBoxField.placeholder = @"请输入邮箱";
        _mailBoxField.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _mailBoxField;

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    self.tabBarController.tabBar.hidden = YES;
    [self.view endEditing:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    self.tabBarController.tabBar.hidden = YES;
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
